unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, DBAccess, Uni, UniProvider, SQLServerUniProvider,
  Dialog_View,Thrad_ImptSORT,UniConnct,UniConfig, RzStatus, ExtCtrls,
  RzPanel,Class_SORT,Class_SORT_MATCH;

type
  TForm1 = class(TDialogView)
    Btnx_EXPT_SORT_2007: TButton;
    UniConnection1: TUniConnection;
    SQLServerUniProvider1: TSQLServerUniProvider;
    Btnx_EXPT_SORT_2010: TButton;
    Btnx_IMPT_SORT: TButton;
    RzStatusBar1: TRzStatusBar;
    RzProgressStatus1: TRzProgressStatus;
    Button1: TButton;
    Button2: TButton;
    procedure Btnx_EXPT_SORT_2007Click(Sender: TObject);
    procedure Btnx_EXPT_SORT_2010Click(Sender: TObject);
    procedure Btnx_IMPT_SORTClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FListSort:TCollection;
    FSaveTabl:string;
  protected
    procedure SetInitialize;override;
    procedure SetCommParams;override;
    procedure SetGridParams;override;
    procedure SetComboItems;override;
    procedure TryFreeAndNil;override;
  public
    procedure CallThradImptSORT(ATabl:string;AList:TCollection);
  public
    //for tkzthrad
    procedure OnKzThradGetMaxProgress(Sender: TObject;AMaxProgress:Integer);
    procedure OnKzThradGetOneProgress(Sender: TObject;AOneProgress:Integer);
    procedure OnKzThradGetTxtProgress(Sender: TObject;ATxtProgress:string);
    procedure OnKzThradGetMsgProgress(Sender: TObject;AMsgProgress:string;AVarProgress:array of Variant);
    procedure OnTerminate(Sender:TObject);
  public
    procedure OnUniConnctCustomConnectionEventByOBJ(AConnectionMark:string;var OBJA:TUniConfig);
  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzUtils,NativeXmlObjectStorage,Dialog_ViewSort,Class_UiUtils,Class_AppUtil;

{$R *.dfm}

procedure TForm1.Btnx_EXPT_SORT_2007Click(Sender: TObject);
var
  SQLA:string;
  List:TCollection;
begin
  try
    SQLA:='SELECT * FROM TBL_SORT  ORDER BY SORT_IDEX';
    List:=TCollection.Create(TSORT);
    TKzUtils.JustCleanList(List);
    TSORT.ListDB(SQLA,Self.UniConnection1,List);
    NativeXmlObjectStorage.ObjectSaveToXmlFile(List,'TBL_SORT_2007.xml');
  finally
    TKzUtils.TryFreeAndNil(List);
  end;
end;

procedure TForm1.Btnx_EXPT_SORT_2010Click(Sender: TObject);
var
  SQLA:string;
  List:TCollection;
begin
  try
    SQLA:='SELECT F1 AS SORT_CODE,F2 AS SORT_NAME,F1 AS SORT_GSDM,F1 AS SORT_YSDM,%s+LEFT(F1,1) AS SORT_TYPE,F4 AS SORT_MEMO FROM SHEET1$ ORDER BY F1';
    SQLA:=Format(SQLA,[QuotedStr('0')]);
    List:=TCollection.Create(TSORT);
    TKzUtils.JustCleanList(List);
    TSORT.ListDB(SQLA,Self.UniConnection1,List);
    NativeXmlObjectStorage.ObjectSaveToXmlFile(List,'TBL_SORT_2010.xml');
  finally
    TKzUtils.TryFreeAndNil(List);
  end;
end;

procedure TForm1.Btnx_IMPT_SORTClick(Sender: TObject);
var
  OD:TOpenDialog;
begin
  try
    OD:=TOpenDialog.Create(nil);
    if OD.Execute then
    begin
      if FListSort=nil then
      begin
        FListSort:=TCollection.Create(TSORT);
      end;
      TKzUtils.JustCleanList(FListSort);
      NativeXmlObjectStorage.ObjectLoadFromXmlFile(FListSort,OD.FileName);
      CallThradImptSORT('TBL_SORT_2010',FListSort);
    end;
  finally
    FreeAndNil(OD);
  end;
end;

procedure TForm1.CallThradImptSORT(ATabl:string;AList:TCollection);
var
  ThradA:TThradImptSORT;
begin
  ThradA:=TThradImptSORT.Create(True);

  ThradA.FListSORT:=AList;
  ThradA.FSaveTabl:=ATabl;
  
  ThradA.OnKzThradGetMaxProgress:=OnKzThradGetMaxProgress;
  ThradA.OnKzThradGetOneProgress:=OnKzThradGetOneProgress;
  ThradA.OnKzThradGetTxtProgress:=OnKzThradGetTxtProgress;

  ThradA.OnKzThradGetMsgProgress:=OnKzThradGetMsgProgress;
  ThradA.OnTerminate            :=OnTerminate;
  ThradA.FreeOnTerminate        :=True; 

  ThradA.Resume; 
end;

procedure TForm1.OnKzThradGetMaxProgress(Sender: TObject;
  AMaxProgress: Integer);
begin
  Self.RzProgressStatus1.TotalParts:=AMaxProgress;
end;

procedure TForm1.OnKzThradGetMsgProgress(Sender: TObject;
  AMsgProgress: string; AVarProgress: array of Variant);
begin

end;

procedure TForm1.OnKzThradGetOneProgress(Sender: TObject;
  AOneProgress: Integer);
begin
  Self.RzProgressStatus1.PartsComplete:=AOneProgress;
end;

procedure TForm1.OnKzThradGetTxtProgress(Sender: TObject;
  ATxtProgress: string);
begin

end;

procedure TForm1.OnTerminate(Sender: TObject);
begin

end;

procedure TForm1.OnUniConnctCustomConnectionEventByOBJ(
  AConnectionMark: string; var OBJA: TUniConfig);
begin
  OBJA.FUnixType:=UniConfig.CONST_PROVIDER_SQLSRV;
  OBJA.FUnixPswd:='sa';
  OBJA.FUnixUser:='sa';
  OBJA.FUnixServ:='127.0.0.1';
  OBJA.FDataBase:='DBZCGL_DQH';
end;

procedure TForm1.SetComboItems;
begin
  inherited;

end;

procedure TForm1.SetCommParams;
begin
  inherited;

end;

procedure TForm1.SetGridParams;
begin
  inherited;

end;

procedure TForm1.SetInitialize;
begin
  inherited;
  UniConnctEx.ConnctMode:=cmByOBJ;
  UniConnctEx.OnUniConnctCustomConnectionEventByOBJ:=OnUniConnctCustomConnectionEventByOBJ;
end;

procedure TForm1.TryFreeAndNil;
begin
  inherited;
  if FListSort<>nil then TKzUtils.TryFreeAndNil(FListSort);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Sort:TSort;
begin
  try
    Sort:=TSort.Create;
    if ViewViewSort('TBL_SORT_2010',Sort)=Mrok then
    begin
      ShowMessage(Sort.SORTNAME);
    end;
  finally
    FreeAndNil(Sort);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  SQLA:string;
  List:TCollection;
begin
  try
    SQLA:='SELECT [F1] AS CODE2007,[F2] AS NAME2007 ,[F3] AS CODE2010 ,[F4] AS NAME2010 FROM [Sheet2$]';
    List:=TCollection.Create(TSortMatch);
    TKzUtils.JustCleanList(List);
    TSortMatch.ListDB(SQLA,Self.UniConnection1,List);
    NativeXmlObjectStorage.ObjectSaveToXmlFile(List,'TBL_SORT_MATCH.xml');
  finally
    TKzUtils.TryFreeAndNil(List);
  end;
end;

end.
